package com.yssoft.controller.core;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yssoft.entity.core.TMaterial;
import com.yssoft.entity.core.TProductClassify;
import com.yssoft.entity.core.TProducts;
import com.yssoft.entity.core.TSemiFinishedGoods;
import com.yssoft.service.core.CodeService;
import com.yssoft.service.core.ProductClassifyService;
import com.yssoft.service.core.ProductsService;
import com.yssoft.service.core.SemiFinishedGoodsService;
import com.yssoft.service.core.TMaterialService;
import com.yssoft.vo.TProductClassifyVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * @author 陈翼东
 * @说明 半成品控制器
 * @time 2018/4/27 上午 10:25
 */
@Controller
@RequestMapping("semiFinishedGoods")
public class SemiFinishedGoodsController {
    
    @Resource
    private SemiFinishedGoodsService semiFinishedGoodsService;
    @Autowired
	private ProductsService productsService;
	@Autowired
	private CodeService codeService;
	@Autowired
	private ProductClassifyService productClassifyService;
	@Resource
	private TMaterialService tMaterialService;
    
    /**
     * 跳转到我企业的部门页面
     * @return
     */
    @RequestMapping("semiFinishedGoodsPage")
    public String departmentPage(){
        return "core/semiFinishedGoods/semiFinishedGoodsList";
    }
    
    /**
     * 分页查询该企业的部门信息
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("querySemiFinishedGoodsPage")
    @ResponseBody
    public JSONObject querySemiFinishedGoodsPage(int page, int limit, String name, String manufacturer) {
        JSONObject jsonObject = semiFinishedGoodsService.querySemiFinishedGoodsPage(page, limit, name, manufacturer);
        return jsonObject;
    }
    
    /**
	 * @方法名称: create
	 * @功能描述: 跳转到新增产品页面
	 * @作者: 洪丰
	 * @创建时间: 2018年4月27日 上午9:57:28
	 * @param model
	 * @return String
	 */
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String create(Model model) {
		/*查询未关联物料信息*/
//		List<TMaterial> notMaterial = tMaterialService.queryMaterialAll();
//		model.addAttribute("notMaterial", notMaterial);
		model.addAttribute("action", "saveProducts");
		model.addAttribute("entity", new TSemiFinishedGoods());
		return "core/semiFinishedGoods/semiFinishedGoodsEdit";
	}
	
	/**
	 * @方法名称: saveProducts
	 * @功能描述: 保存产品
	 * @作者: 洪丰
	 * @创建时间: 2018年4月27日 上午9:59:02
	 * @param entity
	 * @param redirectAttributes
	 * @return String
	 */
	@RequestMapping(value = "saveProducts")
	public String saveProducts(TSemiFinishedGoods entity, String imgId, String tImgId, String tMaterialIds, RedirectAttributes redirectAttributes) {
		try {
			semiFinishedGoodsService.addAcc(entity,imgId,tImgId, tMaterialIds);
			redirectAttributes.addFlashAttribute("message", "保存成功!");
		} catch (Exception e) {
			e.printStackTrace();
			redirectAttributes.addFlashAttribute("message", "保存失败!");
		}
		return "redirect:semiFinishedGoodsPage";
	}
	
	/**
	 * @方法名称: update
	 * @功能描述: 跳转到更新页面
	 * @作者: 洪丰
	 * @创建时间: 2018年4月27日 上午10:07:19
	 * @param id
	 * @param model
	 * @return String
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable("id") Integer id, Model model) {
		TSemiFinishedGoods entity = this.semiFinishedGoodsService.queryProductById(id);
		if(null != entity.getT_p_id()){
			TProductClassify classifyEntity = this.productClassifyService.queryRoleById(entity.getT_p_id());
			model.addAttribute("classifyEntity", classifyEntity);
		}
		model.addAttribute("imgList", semiFinishedGoodsService.findProductImgList(id));
		model.addAttribute("action", "update");
		model.addAttribute("entity", this.semiFinishedGoodsService.queryProductById(id));
		return "core/semiFinishedGoods/semiFinishedGoodsEdit";
	}
	
	/**
	 * @方法名称: view
	 * @功能描述: 查看产品详细
	 * @作者: 洪丰
	 * @创建时间: 2018年4月27日 上午10:08:56
	 * @param id
	 * @param model
	 * @return String
	 */
	@RequestMapping(value = "view/{id}", method = RequestMethod.GET)
	public String view(@PathVariable("id") Integer id, Model model) {
		TSemiFinishedGoods entity = this.semiFinishedGoodsService.queryProductById(id);
		/*查询已关联物料信息*/
//		List<TMaterial> yestMaterial = tMaterialService.queryYesMaterialByALLId(id);
//		model.addAttribute("yestMaterial", yestMaterial);
		if(null != entity.getT_p_id()){
			TProductClassify classifyEntity = this.productClassifyService.queryRoleById(entity.getT_p_id());
			model.addAttribute("classifyEntity", classifyEntity);
		}
		model.addAttribute("imgList", semiFinishedGoodsService.findProductImgList(id));
		model.addAttribute("action", "update");
		model.addAttribute("entity", entity);
		
		return "core/semiFinishedGoods/semiFinishedGoodsView";
	}
	
	/**
	 * @方法名称: update
	 * @功能描述: 修改产品
	 * @作者: 洪丰
	 * @创建时间: 2018年4月27日 上午10:09:41
	 * @param entity
	 * @param redirectAttributes
	 * @return String
	 */
	@RequestMapping(value = "update")
	public String update(TSemiFinishedGoods entity, String imgId, String tImgId, String tMaterialIds, RedirectAttributes redirectAttributes) {
		try {
			semiFinishedGoodsService.editAcc(entity, imgId, tImgId, tMaterialIds);
			redirectAttributes.addFlashAttribute("message", "保存成功!");
		} catch (Exception e) {
			e.printStackTrace();
			redirectAttributes.addFlashAttribute("message", "保存失败!");
		}
		return "redirect:semiFinishedGoodsPage";
	}
	
	/**
	 * @方法名称: delete
	 * @功能描述: 删除产品
	 * @作者: 洪丰
	 * @创建时间: 2018年4月27日 上午10:10:02
	 * @param roleIds
	 * @return String
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete(@RequestParam(value = "ids") String ids) {
		String [] productIdArray = ids.split(",");
		String result = "1";
		for(String id : productIdArray){
			int resultInt = semiFinishedGoodsService.delAccById(Integer.parseInt(id));
			if(resultInt <= 0){
				result = "0";
			}
		}
        return result;
	}
	
	/**
	 * 根据半成品与物料id删除中间数据
	 * @param sId
	 * @param mIds
	 */
	@ResponseBody
	@RequestMapping("/deleteRight")
	public String deleteTSM(String sId, String mIds){
		String result = "1";
		try {
			semiFinishedGoodsService.delSemMater(sId, mIds);
		} catch (Exception e) {
			e.printStackTrace();
			result = "0";
		}
       return result;
	}
	
	/**
	 * 根据半成品与物料id添加中间数据
	 * @param sId
	 * @param mIds
	 */
	@ResponseBody
	@RequestMapping("/insertRight")
	public String insertRight(String sId, String mIds){
		String result = "1";
		try {
			semiFinishedGoodsService.insSemMater(sId, mIds);
		} catch (Exception e) {
			e.printStackTrace();
			result = "0";
		}
       return result;
	}
	
	@RequestMapping("/querySemMater/{id}")
	public String querySemMater(@PathVariable("id") Integer id, Model model) {
		model.addAttribute("sId", id);
		/*查询已关联物料信息*/
		List<Map> yestMaterial = tMaterialService.queryYesMaterialByALLId(id);
		model.addAttribute("yestMaterial", yestMaterial);
		/*查询未关联物料信息*/
		if(yestMaterial.size() > 0){
			List<Map> notMaterial = tMaterialService.queryNoMaterialByALLId(id);
			model.addAttribute("notMaterial", notMaterial);
		} else {
			List<Map> notMaterial = tMaterialService.queryMaterialAll();
			model.addAttribute("notMaterial", notMaterial);
		}
       return "core/semiFinishedGoods/semiFinishedGoodsSelect";
   }
	
	/**
	 * @方法名称: deleteImg
	 * @功能描述: 删除图片
	 * @作者: 洪丰
	 * @创建时间: 2018年4月27日 下午6:43:12
	 * @param imgId
	 * @return String
	 */
	@RequestMapping(value = "deleteImg", method = RequestMethod.POST)
	@ResponseBody
	public String deleteImg(@RequestParam(value = "imgId") String imgId) {
		String result = "1";
		int resultInt = semiFinishedGoodsService.delImgById(Integer.parseInt(imgId));
		if(resultInt <= 0){
			result = "0";
		}
        return result;
	}
}
